<?php

define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);

setlocale(LC_ALL, 'es_MX');
date_default_timezone_set('America/Mexico_City');


if (file_exists(dirname(__FILE__) . '/defines.php')) {
	include_once dirname(__FILE__) . '/defines.php';
}

if (!defined('_JDEFINES')) {
	define('JPATH_BASE', dirname(__FILE__));
	require_once JPATH_BASE.'/includes/defines.php';
	print date('H:i:s')."\tLoading defines...\n";
	
}

require_once JPATH_BASE.DS.'includes'.DS.'framework.php';
require_once JPATH_LIBRARIES.DS.'phpword'.DS.'PHPWord.php';
require_once JPATH_LIBRARIES.DS.'personal'.DS.'Informacion.php';

$db = JFactory::getDbo();
$docx = new PHPWord();
$cfg = new JConfig();
$anio = $cfg->anio;
$quincena = $cfg->quincena;

$q = $db->getQuery(true);

$q->select('m.nof, m.anio, m.fecha_oficio');
$q->select('m.vigencia_del, m.vigencia_al, m.cr, m.descr');
$q->select('m.uresp, m.categoria, m.estructura, m.programa, m.proyecto,m.codigo');
$q->select('m.descr as crdescr');
$q->select('p.rfc, p.apellidop, p.apellidom, p.nombre');
$q->select('p.nacionalidad, p.fecha_nac, p.sexo, p.edo_civil');
$q->select('p.lugar_nac');
$q->select('p.direccion, p.numero_ext, p.numero_int, p.depto, p.colonia');
$q->select('p.municipio, p.cp, p.estado');
$q->select('c.desc_codigo as codescr');
$q->select('c.partida_2 as partida_02, c.partida_42, c.partida_55');
$q->select('e.descr as edescr');
$q->select('h.long_descr as hdescr');

//aditional
$q->select('p.curp');
$q->select('m.movto');

$q->from('personal p');
$q->innerJoin('movimientos m on m.rfc=p.rfc');
$q->innerJoin('cat_codigo c on m.codigo=c.nombre_codigo');
$q->innerJoin('estructuras e on m.estructura = e.estructura');
$q->innerJoin('horarios as h on h.id_horario = m.jornada');

$q->where('1=1');
$q->where('c.anio_codigo='.$anio);
$q->where('p.rfc like "MESG%"');
$q->order('p.rfc');



$db->setQuery($q);
if (!$db->query()) {
	print "\n=*> ERROR: Not query executed.\n";
	echo "$q\n";
	exit(-1);
}

$regs = $db->loadObjectList('rfc');
foreach( $regs as $obj){
	
	$p = new Informacion($obj);
	print "=============================\n";
	print $p->getFullName()."\n";
	print "=============================\n";
}

$p3= $docx->loadTemplate(JPATH_TPL.DS."tpl_oficio_y_contrato_afaspe_2012.docx");

//$p3->populate_AFASPE_2012();

$filename = JPATH_TMP.DS.'dom_merge_'.date('Y_m_d_H_i_s').'.docx';
//$p3->saveDOM($filename);
print "-----------------------------------------\n";
print date('H:i:s')."Saving results in...$filename\n";
print date('H:i:s')."\tFinish\n";